//计算删除后的页码
export function computedPage(deleteCount,CurrentPage,limit,total){
  let pages = Math.ceil((total - deleteCount) / limit)
  if(pages>=CurrentPage){
    return CurrentPage
  }else{
    return pages
  }
}

export function copySome(content,callBack){
  let oInput = document.createElement('input');
  oInput.value = content;
  oInput.style.visibility = '0'
  document.body.appendChild(oInput);
  oInput.select();
  document.execCommand('Copy');
  oInput.remove()
  callBack()
}

export function downloadSome(url,name){
  let downloadA = document.createElement('a');
  downloadA.style.display = 'none'
  downloadA.href = url
  downloadA.setAttribute('download',name)
  downloadA.click()
  downloadA.remove()
}

import html2canvas from 'html2canvas'
export function DomToImg(domElement,name){
  return new Promise((resolve, reject) => {
    try {
      let a = document.createElement('a');
      html2canvas(domElement).then(canvas => {
        let dom = document.body.appendChild(canvas);
        dom.style.display = "none";
        a.style.display = "none";
        document.body.removeChild(dom);
        let arr = dom.toDataURL("image/png").split(',')

        let mime = arr[0].match(/:(.*?);/)[1]
        let bstr = atob(arr[1])
        let n = bstr.length
        let u8arr = new Uint8Array(n);
        while (n--) {
          u8arr[n] = bstr.charCodeAt(n);
        }
        let blob = new Blob([u8arr], {type: mime})
        // let blob = this.dataURLToBlob(dom.toDataURL("image/png"));
        a.setAttribute("href", URL.createObjectURL(blob));
        a.setAttribute("download", name + ".png")
        document.body.appendChild(a);
        a.click();
        URL.revokeObjectURL(blob);
        document.body.removeChild(a);
        resolve(true)
      });
    }catch (e) {
      reject(false)
    }
  })
}

import FileSaver from 'file-saver'
import XLSX from 'xlsx'

export function exportExcel(element,name){
  let wb = XLSX.utils.table_to_book(document.querySelector(element))
  let wbout = XLSX.write(wb, {
    bookType: 'xlsx',
    bookSST: true,
    type: 'array',
  })
  try {
    FileSaver.saveAs(new Blob([wbout], { type: 'application/octet-stream' }), name + '.xlsx')
  } catch (e) {
    if (typeof console !== 'undefined') console.log(e, wbout)
  }
  return wbout
}

// 打包下载图片
import JSZip from 'jszip'
export function downloadImgZip(imgArr, fileName) {
  let zip = new JSZip()
  let imgFolder = zip.folder(fileName)
  let downloadList = []
  for (let i = 0; i < imgArr.length; i++) {
    let image = new Image()
    image.src = imgArr[i].baseImg
    image.onload = ()=>{
      downloadList.push({ name: imgArr[i].name, img: getImgBase64(image).substring(22) })
      if (downloadList.length === imgArr.length) {
        if (downloadList.length > 0) {
          for (let j = 0; j < downloadList.length; j++) {
            imgFolder.file(downloadList[j].name + '.png', downloadList[j].img, { base64: true })
          }
          zip.generateAsync({ type: 'blob' }).then(res=>{
            FileSaver.saveAs(res, fileName + '.zip')
          }).catch(err=>{})
        }
      }
    }
  }
}
// 获取图片base64编码
export function getImgBase64(image){
  let canvas = document.createElement('canvas')
  canvas.width = image.width
  canvas.height = image.height
  let context = canvas.getContext('2d')
  context.drawImage(image, 0, 0, image.width, image.height)
  canvas.toDataURL('image/png')
  return canvas.toDataURL()
}
